<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
		<meta http-equiv="content-type" content="text/html;charset=ISO-8859-1">
	   <link href="/svn_public/templates/BlueGrey/styles.css" rel="stylesheet" media="screen">
      <!--[if gte IE 5.5000]>
      <script type="text/javascript" src="/svn_public/templates/BlueGrey/png.js"></script>
      <![endif]-->
      <title>
         WebSVN
            - dollar_recognizer
               - Rev 2
            - /recognizer_demo.py
      </title>

      <script>
         function checkCB(chBox)
         {
            count = 0
            first = null
            f = chBox.form
            for (i = 0 ; i < f.elements.length ; i++)
            if (f.elements[i].type == 'checkbox' && f.elements[i].checked)
            {
               if (first == null && f.elements[i] != chBox)
                  first = f.elements[i]
               count += 1
            }
            
            if (count > 2) 
            {
               first.checked = false
               count -= 1
            }
         }
      </script>

   </head>
   <body>

      <div style="text-align: center">
        <a href="http://subversion.tigris.org/"><img style="border: 0; width: 468px; height: 64px" src="/svn_public/templates/BlueGrey/subversion.png" alt="Subversion" /></a>
      </div>
      
      <hr>

<div align="right"><form action="/svn_public/wsvn" method="post" name="projectform"><select name="repname" onchange="javascript:this.form.submit();"><option value="dcuttxml" >dcuttxml</option><option value="dollar_recognizer" selected>dollar_recognizer</option><option value="freespace" >freespace</option><option value="idlers-irssi" >idlers-irssi</option><option value="twitter-irssi" >twitter-irssi</option></select><input type="submit" value="Go"><input type="hidden" name="selectproj" value="1"><input type="hidden" name="op" value="form"><input type="hidden" name="sc" value="0"></form></div>
<h2>dollar_recognizer</h2>
[<a href="/svn_public/wsvn/dollar_recognizer/?rev=0&amp;sc=0">/]</a> [<b>recognizer_demo.py</b>] - Rev 2
<p>
<a href="/svn_public/wsvn/dollar_recognizer/recognizer_demo.py?op=diff&amp;rev=0&amp;sc=0">Compare with Previous</a> - <a href="/svn_public/wsvn/dollar_recognizer/recognizer_demo.py?op=blame&amp;rev=0&amp;sc=0">Blame</a>
<p>
<hr>
<table width="100%" border=0><tr><td class="row0">
<PRE>
<I><FONT COLOR="#B22222">#!/usr/bin/env python
</FONT></I><I><FONT COLOR="#B22222">#
</FONT></I><I><FONT COLOR="#B22222"># Simple demo of the $1 gesture recognizer.
</FONT></I><I><FONT COLOR="#B22222"># Requires WxPython.
</FONT></I><I><FONT COLOR="#B22222">#
</FONT></I>
<B><FONT COLOR="#A020F0">import</FONT></B> wx
<B><FONT COLOR="#A020F0">from</FONT></B> dollar <B><FONT COLOR="#A020F0">import</FONT></B> Recognizer

<I><FONT COLOR="#B22222"># Raw points for the three example gestures.
</FONT></I>circlePoints = [(269, 84), (263, 86), (257, 92), (253, 98), (249, 104), (245, 114), (243, 122), (239, 132), (237, 142), (235, 152), (235, 162), (235, 172), (235, 180), (239, 190), (245, 198), (251, 206), (259, 212), (267, 216), (275, 218), (281, 222), (287, 224), (295, 224), (301, 226), (311, 226), (319, 226), (329, 226), (339, 226), (349, 226), (352, 226), (360, 226), (362, 225), (366, 219), (367, 217), (367, 209), (367, 206), (367, 198), (367, 190), (367, 182), (367, 174), (365, 166), (363, 158), (359, 152), (355, 146), (353, 138), (349, 134), (345, 130), (341, 124), (340, 122), (338, 121), (337, 119), (336, 117), (334, 116), (332, 115), (331, 114), (327, 110), (325, 109), (323, 109), (321, 108), (320, 108), (318, 107), (316, 107), (315, 107), (314, 107), (313, 107), (312, 107), (311, 107), (310, 107), (309, 106), (308, 106), (307, 105), (306, 105), (305, 105), (304, 105), (303, 104), (302, 104), (301, 104), (300, 104), (299, 103), (298, 103), (296, 102), (295, 101), (293, 101), (292, 100), (291, 100), (290, 100), (289, 100), (288, 100), (288, 99), (287, 99), (287, 99)]
squarePoints = [(193, 123), (193, 131), (193, 139), (195, 151), (197, 161), (199, 175), (201, 187), (205, 201), (207, 213), (209, 225), (213, 235), (213, 243), (215, 251), (215, 254), (217, 262), (217, 264), (217, 266), (217, 267), (218, 267), (219, 267), (221, 267), (224, 267), (227, 267), (237, 267), (247, 265), (259, 263), (273, 261), (287, 261), (303, 259), (317, 257), (331, 255), (347, 255), (361, 253), (375, 253), (385, 253), (395, 251), (403, 249), (406, 249), (408, 249), (408, 248), (409, 248), (409, 246), (409, 245), (409, 242), (409, 234), (409, 226), (409, 216), (407, 204), (407, 194), (405, 182), (403, 172), (403, 160), (401, 150), (399, 140), (399, 130), (397, 122), (397, 119), (397, 116), (396, 114), (396, 112), (396, 111), (396, 110), (396, 109), (396, 108), (396, 107), (396, 106), (396, 105), (394, 105), (392, 105), (384, 105), (376, 105), (364, 105), (350, 107), (334, 109), (318, 111), (306, 113), (294, 115), (286, 117), (278, 117), (272, 119), (269, 119), (263, 121), (260, 121), (254, 123),&nbsp;(251, 123), (245, 125), (243, 125), (242, 125), (241, 126), (240, 126), (238, 127), (236, 127), (232, 128), (231, 128), (231, 129), (230, 129), (228, 129), (227, 129), (226, 129), (225, 129), (224, 129), (223, 129), (222, 129), (221, 130), (221, 130)]
trianglePoints = [(282, 83), (281, 85), (277, 91), (273, 97), (267, 105), (261, 113), (253, 123), (243, 133), (235, 141), (229, 149), (221, 153), (217, 159), (216, 160), (215, 161), (214, 162), (216, 162), (218, 162), (221, 162), (227, 164), (233, 166), (241, 166), (249, 166), (259, 166), (271, 166), (283, 166), (297, 166), (309, 164), (323, 164), (335, 162), (345, 162), (353, 162), (361, 160), (363, 159), (365, 159), (366, 158), (367, 158), (368, 157), (369, 157), (370, 156), (371, 156), (371, 155), (372, 155), (372, 153), (372, 152), (372, 151), (372, 149), (372, 147), (371, 145), (367, 141), (363, 137), (359, 133), (353, 129), (349, 125), (343, 121), (337, 119), (333, 115), (327, 111), (325, 110), (324, 109), (320, 105), (318, 104), (314, 100), (312, 99), (310, 98), (306, 94), (305, 93), (303, 92), (301, 91), (300, 90), (298, 89), (297, 88), (296, 88), (295, 87), (294, 87), (293, 87), (293, 87)]

<B><FONT COLOR="#A020F0">class</FONT></B> RecognizerDemoWindow(wx.Frame):
&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">__init__</FONT></B>(self, parent, id, title, width, height):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.frame = wx.Frame.__init__(self, parent, id, title, size = wx.Size(width, height))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.SetMinSize((width, height))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.SetMaxSize((width, height))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.Bind(wx.EVT_PAINT, self.OnPaint)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.Bind(wx.EVT_LEFT_DOWN, self.OnMouseDown)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.Bind(wx.EVT_MOTION, self.OnMouseMove)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.Bind(wx.EVT_LEFT_UP, self.OnMouseUp)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.mouseDown = False
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.positions = []

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.recognizer = Recognizer()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.recognizer.addTemplate(<B><FONT COLOR="#BC8F8F">'circle'</FONT></B>, circlePoints)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.recognizer.addTemplate(<B><FONT COLOR="#BC8F8F">'square'</FONT></B>, squarePoints)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.recognizer.addTemplate(<B><FONT COLOR="#BC8F8F">'triangle'</FONT></B>, trianglePoints)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_name = None
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_accuracy = 0.0

&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">OnPaint</FONT></B>(self, event):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc = wx.BufferedPaintDC(self)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.Clear()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">for</FONT></B> position <B><FONT COLOR="#A020F0">in</FONT></B> self.positions:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x, y, type) = position

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">if</FONT></B> type == <B><FONT COLOR="#BC8F8F">&quot;start&quot;</FONT></B>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = 10
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">elif</FONT></B> type == <B><FONT COLOR="#BC8F8F">&quot;stop&quot;</FONT></B>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = 1

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;points = [(p[0], p[1]) <B><FONT COLOR="#A020F0">for</FONT></B> p <B><FONT COLOR="#A020F0">in</FONT></B> self.positions]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">if</FONT></B> len(points) &gt; 10:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(name, score) = self.recognizer.recognize(points)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_name = name
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_accuracy = score
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">else</FONT></B>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_name = <B><FONT COLOR="#BC8F8F">'(Not enough points - try again!)'</FONT></B>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.last_accuracy = 0.0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">else</FONT></B>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = 3

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.DrawCircle(x, y, r)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.DrawText(<B><FONT COLOR="#BC8F8F">&quot;$1 gesture recognizer demo&quot;</FONT></B>, 10, 10)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.DrawText(<B><FONT COLOR="#BC8F8F">&quot;Gestures: circle, square, triangle&quot;</FONT></B>, 20, 30)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.DrawText(<B><FONT COLOR="#BC8F8F">&quot;Last drawn gesture: %s&quot;</FONT></B> % self.last_name, 20, 60)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dc.DrawText(<B><FONT COLOR="#BC8F8F">&quot;Gesture accuracy: %2.2f%%&quot;</FONT></B> % (self.last_accuracy * 100), 20, 80)

&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">OnMouseDown</FONT></B>(self, event):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.mouseDown = True
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x, y) = event.GetPosition()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.positions = [(x, y, <B><FONT COLOR="#BC8F8F">'start'</FONT></B>)]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.OnPaint(None)

&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">OnMouseMove</FONT></B>(self, event):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">if</FONT></B> self.mouseDown:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x, y) = event.GetPosition()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.positions.append((x, y, <B><FONT COLOR="#BC8F8F">'move'</FONT></B>))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.OnPaint(None)
&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">OnMouseUp</FONT></B>(self, event):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.mouseDown = False
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(x, y) = event.GetPosition()
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.positions.append((x, y, <B><FONT COLOR="#BC8F8F">'stop'</FONT></B>))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.OnPaint(None)

<B><FONT COLOR="#A020F0">class</FONT></B> RecognizerDemo(wx.App):
&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">def</FONT></B> <B><FONT COLOR="#0000FF">OnInit</FONT></B>(self):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.frame = RecognizerDemoWindow(None, -1, <B><FONT COLOR="#BC8F8F">&quot;$1 gesture recognizer demo&quot;</FONT></B>, 600, 400)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.frame.Show(True)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.SetTopWindow(self.frame)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<B><FONT COLOR="#A020F0">return</FONT></B> True

demo = RecognizerDemo()
demo.MainLoop()



</PRE>
</td></tr></table>
<hr>

      <p><center><font size="-1"><i><b>Powered by <a href="http://websvn.tigris.org/">WebSVN</a> v1.61</b></i></font></center>
   </body>
</html>
